package com.qianhong.crm.workbench.web.controller;

import com.qianhong.crm.commons.contanls.Contacts;
import com.qianhong.crm.commons.domain.ReturnObject;
import com.qianhong.crm.commons.utils.DateUtils;
import com.qianhong.crm.commons.utils.UUIDUtils;
import com.qianhong.crm.settings.domain.User;
import com.qianhong.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class RegisterController {
  /*  @RequestMapping("/index01.do")
    public String index01(){

        return "index01";
    }*/
    @Autowired
    private UserService userService;
    @RequestMapping("/saveRegister.do")
    @ResponseBody
    public Object saveRegister(User user , HttpServletRequest request) {
        ReturnObject returnObject = new ReturnObject();
        User user1 = userService.selectUser(user.getLoginAct());
        if (user1 != null) {
            returnObject.setCode(Contacts.ReTURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("登录账号以存在！滚");
        } else {
            user.setId(UUIDUtils.getUUID());
            user.setCreatetime(DateUtils.formatDateTime(new Date()));
            user.setCreateBy(user.getName());
            user.setLockState(Contacts.REMARK_EDIT_FlAG_YES_EDITED);  //  登录
//        user.setLockState(Contacts.REMARK_EDIT_FlAG_NO_EDITED);  //  不可登录
//        user.setAllowIps(request.getRemoteAddr());  request.getRemoteAddr()
            user.setAllowIps("192.168.1.1,0:0:0:0:0:0:0:1");
            user.setExpireTime("2023-11-27 21:50:05");
            user.setDeptno("A001");
            System.out.println("email :" + user.getEmail());
            System.out.println("username :" + user.getName());
            System.out.println("loginAct :" + user.getLoginAct());
            System.out.println("password1 :" + user.getLoginPwd());


            try {
                int count = userService.saveInsertInto(user);
                if (count > 0) {
                    returnObject.setCode(Contacts.ReTURN_OBJECT_CODE_SUCCESS);
                    returnObject.setRetData(user.getId());
                    returnObject.setMessage("注册成功！");
                } else {
                    System.out.println("-------------------------------------------------------------------");
                    System.out.println(count);
                    System.out.println("-------------------------------------------------------------------");
                    returnObject.setCode(Contacts.ReTURN_OBJECT_CODE_FAIL);
                    returnObject.setMessage("注册失败！");
                }
            } catch (Exception e) {
                e.printStackTrace();
                returnObject.setCode(Contacts.ReTURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("注册失败！出现异常");
            }
        }
            return returnObject;
    }
}
